package com.robosense.datacenter.result;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Map;
import java.util.Optional;

public class RestUtils {
    private RestTemplate restTemplate;
    private static RestUtils restUtils = null;

    private RestUtils() {
        restTemplate = new RestTemplate();
        Optional<HttpMessageConverter<?>> converter = restTemplate.getMessageConverters().stream().filter(c -> c instanceof StringHttpMessageConverter).findAny();
        converter.ifPresent(httpMessageConverter -> ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName("UTF-8")));
    }

    public static RestUtils getInstance() {
        synchronized (RestUtils.class) {
            if (null == restUtils) {
                restUtils = new RestUtils();
            }
            return restUtils;
        }
    }


    /**
     * RestTemplate 的get请求封装
     *
     * @param url          网址如 String url = "http://localhost:7012/data/api/leave?currentPage={page}&pageSize={pageSize}";
     * @param parasMap     参数key是{page},value是具体的数值
     * @param responseType 响应类型 如String,自定义实体等
     * @param <T>          如String,自定义实体等
     * @return 如String, 自定义实体等
     */
    public <T> T get(String url, Map<String, Object> parasMap, Class<T> responseType) {
        return restTemplate.getForObject(url, responseType, parasMap);
    }


    /**
     * RestTemplate 的post请求封装
     *
     * @param url          如String url = "http://localhost:7012/data/api/leave";
     * @param paramEntity  实体封装
     * @param responseType 响应类型 如String,自定义实体等
     * @param <T>          如String,自定义实体等
     * @return 如String, 自定义实体等
     */
    public <T> ResponseEntity<T> post(String url, Object paramEntity, Class<T> responseType) {
        return restTemplate.postForEntity(url, paramEntity, responseType);
    }

    /**
     * RestTemplate 的post请求封装
     *
     * @param url          如 http://10.10.5.77:80/api/v2/employee/update/?key={key}
     * @param paramEntity  实体封装
     * @param paramMap     参数key是{page},value是具体的数值
     * @param responseType 响应类型 如String,自定义实体等
     * @param <T>          如String,自定义实体等
     * @return 如String, 自定义实体等
     */
    public <T> ResponseEntity<T> post(String url, Object paramEntity, Map<String, Object> paramMap, Class<T> responseType) {
        return restTemplate.postForEntity(url, paramEntity, responseType, paramMap);
    }

    /**
     * RestTemplate 的post请求封装
     *
     * @param url          如 http://10.10.5.77:80/api/v2/employee/update/?key={key}
     * @param paramEntity  实体封装
     * @param paramMap     参数key是{page},value是具体的数值
     * @param httpHeaders  请求头类型设置
     * @param responseType 响应类型 如String,自定义实体等
     * @param <T>          如String,自定义实体等
     * @return 如String, 自定义实体等
     */
    public <T> ResponseEntity<T> post(String url, Object paramEntity, Map<String, Object> paramMap, Class<T> responseType, HttpHeaders httpHeaders) throws JsonProcessingException {
        ObjectMapper om = new ObjectMapper();
        String value = om.writeValueAsString(paramEntity);
        System.out.println(value);
        HttpEntity<String> entity = new HttpEntity<>(value, httpHeaders);
        return restTemplate.postForEntity(url, entity, responseType, paramMap);
    }

    /**
     * RestTemplate 的delete请求封装
     *
     * @param url      如"http://localhost:7012/data/api/leave/{leaveId}
     * @param paramMap key是leaveId的Map
     */
    public void delete(String url, Map<String, Object> paramMap) {
        restTemplate.delete(url, paramMap);
    }


    public <T> ResponseEntity<T> exchange(String url, HttpMethod httpMethod, Object paramEntity, Map<String, Object> paramMap, Class<T> responseType) {
        HttpEntity<Object> entity = null;
        if (paramEntity != null) {
            HttpHeaders header = new HttpHeaders();
            header.setContentType(MediaType.APPLICATION_JSON);
            entity = new HttpEntity<>(paramEntity, header);
        }
        return restTemplate.exchange(url, httpMethod, entity, responseType, paramMap);
    }

}
